$(function () {
  var username = $(".username input");
  var password = $(".passward input");
  var surepass = $(".sure_pass input");
  var btn = $("button");
  var usern = "";
  let reg = /^\d{6}$/;
  // 验证用户名
  username.on("change", function () {
    usern = $(this).val();
    if ($(this).val()) {
      var checknameAPI = "http://jx.xuzhixiang.top/ap/api/checkname.php";
      axios
        .get(checknameAPI, {
          params: { username: $(this).val() },
        })
        .then(function (res) {
          if (res.data.code == 0) {
            $(".username input").val("");
            alert("用户名已经存在");
          }
        });
    }
  });

  //验证密码是否位6位数字
  password.on("change", function () {
    if (reg.test(password.val())) {
      return;
    } else {
      $(".password input").val("");
      alert("请输入6位数字密码");
    }
  });

  // 验证两次密是否一致
  surepass.on("change", function () {
    if (password.val() == surepass.val()) {
      return;
    } else {
      alert("两次输入不一致");
    }
  });
  // 将用户名和密码存储下来
  btn.on("click", function () {
    username = username.val();
    password = password.val();
    console.log(username);
    console.log(password);
    var regAPI = "http://jx.xuzhixiang.top/ap/api/reg.php";
    axios
      .get(regAPI, { params: { username, password } })
      .then(function (res) {
        console.log(res);
        if (res.data.code == 1) {
          alert("注册登录成功，2s后跳转到天猫首页");
          setInterval(function () {
            location.href = `index.html?uname=${username}`;
          }, 2000);
        }
        // location.href="login.html"
      })
      .catch(function (err) {
        console.log(err);
      });
  });
})();
